package db

import (
	"fmt"
	"sftti-go-service/types"

	"github.com/dubbogo/gost/log/logger"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var DB *sqlx.DB

func InitDb(dbConfig types.DatabaseConfig) {

	var err error
	var dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		dbConfig.Database.Username, dbConfig.Database.Password,
		dbConfig.Database.Host, dbConfig.Database.Port, dbConfig.Database.Dbname)

	DB, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		panic("failed to connect database")
	}

	DB.SetMaxOpenConns(20) //设置最大连接数
	DB.SetMaxIdleConns(10) //设置最大空闲连接数

	logger.Infof("###Init Db Ok.")
}
